import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { ApiProperty } from '@nestjs/swagger';

// 商品状态枚举
export enum ProductStatus {
  ON_SALE = 'on_sale', // 在售
  OFF_SALE = 'off_sale', // 下架
}

//商品类型-新品，热卖，常规
export enum ProductType {
  NEW = 'new', // 新品
  HOT = 'hot', // 热卖
  COMMON = 'common', // 常规
}

@Schema({
  timestamps: true, // 自动添加createdAt和updatedAt字段
  collection: 'product', // 集合名称
  versionKey: false, // 不返回版本字段
})
export class Product extends Document {
  @ApiProperty({ description: '商品名称' })
  @Prop({ required: true, type: String })
  name: string;

  @ApiProperty({ description: '所属分类ID' })
  @Prop({
    required: true,
    type: Types.ObjectId,
    ref: 'category',
    index: true,
  })
  categoryId: Types.ObjectId;

  @ApiProperty({ description: '销售价格' })
  @Prop({ required: true, type: Number, min: 0 })
  sellingPrice: number;

  @ApiProperty({ description: '原价' })
  @Prop({ type: Number, min: 0 })
  originalPrice: number;

  @ApiProperty({ description: '是否是热门' })
  @Prop({ type: Boolean, default: false })
  isHot: boolean;

  @ApiProperty({
    description: '商品状态',
    enum: ProductStatus,
    default: ProductStatus.ON_SALE,
  })
  @Prop({
    required: true,
    type: String,
    enum: ProductStatus,
    default: ProductStatus.ON_SALE,
  })
  status: ProductStatus;

  @ApiProperty({ description: '商品展示图URL' })
  @Prop({ required: true, type: String })
  mainImage: string;

  @ApiProperty({ description: '商品详情图URL数组，最多5张' })
  @Prop({
    required: true,
    type: [String],
    validate: {
      validator: function (v: string[]) {
        return v.length <= 5;
      },
      message: '商品详情图最多只能有5张',
    },
  })
  detailImages: string[];

  @ApiProperty({ description: '商品简介' })
  @Prop({ required: true, type: String })
  simpleDesc: string;

  @ApiProperty({ description: '商品详细描述' })
  @Prop({ required: true, type: String })
  description: string;

  @ApiProperty({ description: '商品特色数组' })
  @Prop({ required: true, type: [String] })
  features: string[];

  @ApiProperty({ description: '配料信息', required: false })
  @Prop({ type: String })
  ingredients?: string;
}

export const ProductSchema = SchemaFactory.createForClass(Product);
